package Parser;
/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */

import java.util.*;

public class MalformedStack {
    private ArrayList<MalformedStackElement> list = new ArrayList<MalformedStackElement>();

    public MalformedStack() {
    }

    public void push(Token datum)
    {
        list.add(new MalformedStackElement(datum, false));
    }

    public void push(Token datum, boolean marked)
    {
        list.add(new MalformedStackElement(datum, marked));
    }

    public void push(Token datum, boolean marked, int index)
    {
        list.add(new MalformedStackElement(datum, marked, index));
    }

    public void push(Token datum, int index) {
        list.add(new MalformedStackElement(datum, false, index));
    }

    public boolean isTopMarked()
    {
        return list.get(list.size() - 1).isMarked();
    }

    public Token pop()
    {
        return list.remove(list.size() - 1).getToken();
    }

    public MalformedStackElement popFull()
    {
        return list.remove(list.size() - 1);
    }

    public Token peek()
    {
        return list.get(list.size() - 1).getToken();
    }

    public Token pop(Token close)
    {
        for (int c = list.size() - 1; c >= 0; c--)
        {
            if (close.closes(list.get(c).getToken()))
            {
                return list.remove(c).getToken();
            }
        }

        return null;
    }

    public MalformedStackElement popFull(Token close)
    {
        for (int c = list.size() - 1; c >= 0; c--)
        {
            if (close.closes(list.get(c).getToken()))
            {
                return list.remove(c);
            }
        }

        return null;
    }


    public boolean isEmpty()
    {
        return list.size() == 0;
    }
}
